
const $ = ele => document.querySelector(ele)

const ajax = (path , data = {} , cb) => {

    // 把数据对象变成字符串  a=11&b=22
    let params = '' ;
    for(let key in data) {
        params += `${key}=${data[key]}&` ;
    }
    params = params.substring(0 , params.length - 1) ;
    console.log(params);

    const xhr = new XMLHttpRequest() ;
    xhr.open('post' , path , true) ;
    // 设置请求头
    xhr.setRequestHeader('content-type' ,'application/x-www-form-urlencoded');
    xhr.send(params) ;
    xhr.onreadystatechange = function () {  
        if(xhr.readyState === 4 && xhr.status === 200) {
            const data = xhr.responseText ;
            cb(data) ;
        }
    }
}